package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.bifang.engine.data.core.util.PagerUtil;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.common.vo.excel.ExcelModelVO;
import com.bifang.module.base.common.vo.excel.ExcelVO;
import com.bifang.module.base.common.vo.operationPeriod.OperationCurVO;
import com.bifang.module.base.common.vo.operationPeriod.OperationPeriodVO;
import com.bifang.module.base.core.helper.ApiContainer;
import com.bifang.module.base.core.helper.ExcelCommonHelper;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.bifang.module.base.core.integration.OperationPeriodIntegration;
import com.bifang.module.system.core.integration.impl.ExcelIntegrationImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.enums.SchoolCategoryEnum;
import com.jwsoft.manager.common.vo.eduHouse.EduHouseVO;
import com.jwsoft.manager.common.vo.eduHouse.HouseUsageRecordVO;
import com.jwsoft.manager.common.vo.eduHouse.StudentHouseQueryVO;
import com.jwsoft.manager.common.vo.eduStudentHouse.EduStudentHouseKeyVO;
import com.jwsoft.manager.common.vo.eduStudentHouse.EduStudentHouseQueryByRealpVO;
import com.jwsoft.manager.common.vo.eduStudentHouse.EduStudentHouseQueryVO;
import com.jwsoft.manager.common.vo.eduStudentHouse.EduStudentHouseVO;
import com.jwsoft.manager.core.dao.model.EduStudentHouse;
import com.jwsoft.manager.core.dao.service.EduStudentHouseService;
import com.jwsoft.manager.core.integration.EduStudentHouseIntegration;
import com.jwsoft.manager.core.util.EduDesensitizeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 录取学生房产信息表业务实现类
 *
 * @author wangtao
 * @since 2022-12-01
 */
@ApiService(funcCode = "eduStudentHouse", title = "录取学生房产信息表")
public class EduStudentHouseIntegrationImpl implements EduStudentHouseIntegration {

    @Autowired
    public EduStudentHouseService eduStudentHouseService;

    @Autowired
    OperationPeriodIntegration operationPeriodIntegration;
    @Autowired
    CommonIntegration commonIntegration;

    @Resource
    public ExcelIntegrationImpl excelIntegration;

    @Autowired
    ExcelCommonHelper excelCommonHelper;

    @Resource
    private ApiContainer apiContainer;
    @Value("${spring.servlet.multipart.location}")
    private String tempPath;

    @Override
    @OpApi(funcCode = "eduStudentHouse0001", title = "录取学生房产信息表分页查询", funcType = FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public PageInfo<EduStudentHouseVO> getList(EduStudentHouseQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduStudentHouse> list = eduStudentHouseService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentHouse> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentHouseVO> resultList = Convert.toList(EduStudentHouseVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0002", title = "录取学生房产信息表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduStudentHouseVO getById(EduStudentHouseKeyVO vo) {
        EduStudentHouse entity = eduStudentHouseService.getById(vo.getStudentHouseId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduStudentHouseVO result = Convert.convert(EduStudentHouseVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduStudentHouse0003", title = "录取学生房产信息表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduStudentHouseVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0004", title = "录取学生房产信息表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduStudentHouse0004",
            operationName = "录取学生房产信息表新增",
            dataType = "studentHouseId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentHouseVO add(EduStudentHouseVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentHouse entity = Convert.convert(EduStudentHouse.class, vo);
        eduStudentHouseService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setStudentHouseId(entity.getStudentHouseId());
        clearCache(entity.getRealProperty());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0005", title = "录取学生房产信息表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduStudentHouse0005",
            operationName = "录取学生房产信息表修改",
            dataType = "studentHouseId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentHouseVO edit(EduStudentHouseVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentHouseId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentHouse oldEntity = eduStudentHouseService.getById(vo.getStudentHouseId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentHouse entity = Convert.convert(EduStudentHouse.class, vo);
        eduStudentHouseService.updateById(entity);
        clearCache(entity.getRealProperty());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0006", title = "录取学生房产信息表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduStudentHouse0006",
            operationName = "录取学生房产信息表删除",
            dataType = "studentHouseId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduStudentHouseKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentHouseId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduStudentHouse entity = eduStudentHouseService.getById(vo.getStudentHouseId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduStudentHouseService.removeById(vo.getStudentHouseId());
        clearCache(entity.getRealProperty());
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0007", title = "学生房产使用情况查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduStudentHouseVO> queryStudentHouseInfo(StudentHouseQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        //取出脱敏字段，判断是否为空
        Boolean desensitizeFlag = vo.getDesensitizeFlag();
        if (desensitizeFlag == null){
            desensitizeFlag = true;
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        QueryWrapper<EduStudentHouse> eduStudentHouseQueryWrapper = constructQueryWrapper(vo);

        List<EduStudentHouse> list = eduStudentHouseService.list(eduStudentHouseQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentHouse> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentHouseVO> resultList = Convert.toList(EduStudentHouseVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        //脱敏
        if (desensitizeFlag){
            for (EduStudentHouseVO eduStudentHouseVO : resultList) {
                eduStudentHouseVO.setIdcard(EduDesensitizeUtil.desensitizeIdcard(eduStudentHouseVO.getIdcard()));
                eduStudentHouseVO.setFullName(EduDesensitizeUtil.desensitizeFullName(eduStudentHouseVO.getFullName()));
                eduStudentHouseVO.setFatherIdcard(EduDesensitizeUtil.desensitizeIdcard(eduStudentHouseVO.getFatherIdcard()));
                eduStudentHouseVO.setMotherIdcard(EduDesensitizeUtil.desensitizeIdcard(eduStudentHouseVO.getMotherIdcard()));
                eduStudentHouseVO.setRealProperty(EduDesensitizeUtil.desensitizeOther(eduStudentHouseVO.getRealProperty()));
                eduStudentHouseVO.setFatherName(EduDesensitizeUtil.desensitizeFullName(eduStudentHouseVO.getFatherName()));
                eduStudentHouseVO.setMotherName(EduDesensitizeUtil.desensitizeFullName(eduStudentHouseVO.getMotherName()));
                eduStudentHouseVO.setAddress(EduDesensitizeUtil.desensitizeAddress(eduStudentHouseVO.getAddress()));

            }
        }
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    private QueryWrapper<EduStudentHouse> constructQueryWrapper(StudentHouseQueryVO vo) {
        QueryWrapper<EduStudentHouse> eduStudentHouseQueryWrapper = new QueryWrapper<>();
        eduStudentHouseQueryWrapper
                .lambda()
                .eq(!ObjectUtils.isEmpty(vo.getStatus()),
                        EduStudentHouse::getStatus,
                        BoolEnum.TRUE.getType())
                .eq(
                        !ObjectUtils.isEmpty(vo.getHouseId()),
                        EduStudentHouse::getHouseId,
                        vo.getHouseId())
                .like(
                        !ObjectUtils.isEmpty(vo.getAddress()),
                        EduStudentHouse::getAddress,
                        Convert.toDBC(vo.getAddress()))
                .eq(
                        !ObjectUtils.isEmpty(vo.getOrgId()),
                        EduStudentHouse::getFinalSchoolId,
                        vo.getOrgId())
                .eq(
                        !ObjectUtils.isEmpty(vo.getPersonId()),
                        EduStudentHouse::getPersonId,
                        vo.getPersonId())
                .eq(
                        !ObjectUtils.isEmpty(vo.getFatherId()),
                        EduStudentHouse::getFatherId,
                        vo.getFatherId())
                .eq(
                        !ObjectUtils.isEmpty(vo.getMotherId()),
                        EduStudentHouse::getMotherId,
                        vo.getMotherId())
                .like(
                        !ObjectUtils.isEmpty(vo.getRealProperty()),
                        EduStudentHouse::getRealProperty,
                        vo.getRealProperty())
                .eq(!ObjectUtils.isEmpty(vo.getYear()), EduStudentHouse::getYear, vo.getYear())
                .eq(!ObjectUtils.isEmpty(vo.getAreaCode()),EduStudentHouse::getAreaCode,vo.getAreaCode());

        if(StringUtils.isNotBlank(vo.getUseFlag())){
            int year = 0;

            OperationCurVO curVO = new OperationCurVO();
            curVO.setOperating("XN");
            OperationPeriodVO operationPeriodVO =
                    operationPeriodIntegration.getCurrentByOperating(curVO);
            if (operationPeriodVO != null) {
                year = operationPeriodVO.getPeriodYear();
            } else {
                year = DateUtil.thisYear();
            }
            year=year-3;
            if(BoolEnum.TRUE.getType().equalsIgnoreCase(vo.getUseFlag())){
                eduStudentHouseQueryWrapper
                        .lambda().ge( EduStudentHouse::getYear, year);
            }else{
                eduStudentHouseQueryWrapper
                        .lambda().lt( EduStudentHouse::getYear, year);
            }
        }

        return eduStudentHouseQueryWrapper;
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0008", title = "学生房产使用情况查询(通过房产证号)", funcType = FuncTypeEnum.query)
    public EduHouseVO getListByRealp(EduStudentHouseQueryByRealpVO vo) {
        if(StringUtils.isBlank(vo.getRealProperty())){
            throw new AppException("不动产权证号不能为空");
        }
        vo.setRealProperty(Convert.toDBC(vo.getRealProperty()));
        List<EduStudentHouseVO> list = eduStudentHouseService.getListByRealp(vo);
        if(!CollectionUtils.isEmpty(list)){
            EduHouseVO eduHouseVO=getUserHouse(list,vo.getHiddenFlag());
            return eduHouseVO;
        }
        return null;

    }
    private EduHouseVO getUserHouse(List<EduStudentHouseVO> list,Boolean hiddenFlag){
        EduHouseVO eduHouseVO = Convert.convert(EduHouseVO.class,list.get(0));
        if(hiddenFlag == null){
            hiddenFlag=false;
        }
        if(hiddenFlag) {
            String hiderealProperty = "";
            String hiderealPropertyNum = "";
            for (int i = 0; i < eduHouseVO.getRealProperty().length(); i++) {
                if (Character.isDigit(eduHouseVO.getRealProperty().charAt(i))) {
                    hiderealProperty = hiderealProperty + "*";
                } else {
                    hiderealProperty = hiderealProperty + eduHouseVO.getRealProperty().substring(i, i + 1);
                }
            }
            if (eduHouseVO.getRealPropertyNum() != null) {
                int len = eduHouseVO.getRealPropertyNum().length();
                if (len > 8) {
                    hiderealPropertyNum = eduHouseVO.getRealPropertyNum().substring(0, 4) + StringUtils.leftPad(eduHouseVO.getRealPropertyNum().substring(len - 4, len), len - 8, "*");
                } else {
                    hiderealPropertyNum = eduHouseVO.getRealPropertyNum().substring(0, 1) + "******" + eduHouseVO.getRealPropertyNum().substring(len - 1, len);
                }
            }
            eduHouseVO.setRealProperty(hiderealProperty);
            eduHouseVO.setRealPropertyNum(hiderealPropertyNum);

        }
        List<HouseUsageRecordVO> recordVOArrayList = new ArrayList<>();
        for (EduStudentHouseVO eduStudentHouse : list) {
            HouseUsageRecordVO houseUsageRecordVO =
                    Convert.convert(HouseUsageRecordVO.class, eduStudentHouse);
            houseUsageRecordVO.setYear(eduStudentHouse.getYear());
            if(hiddenFlag) {
                houseUsageRecordVO.setAddress(eduStudentHouse.getAddress());
                if(StringUtils.isNotBlank(eduStudentHouse.getFullName())){
                    houseUsageRecordVO.setStudentName(
                            '*'+eduStudentHouse.getFullName().substring(1));
                }
            }else{
                houseUsageRecordVO.setStudentName(eduStudentHouse.getFullName());
            }
            houseUsageRecordVO.setSchoolId(eduStudentHouse.getFinalSchoolId());

            houseUsageRecordVO.setSchoolName(eduStudentHouse.getSchoolName());

            if (houseUsageRecordVO.getSchoolCategory() != null) {
                if (houseUsageRecordVO.getSchoolCategory().equals(SchoolCategoryEnum.KINDERGARTEN.getType())){
                    houseUsageRecordVO.setSchoolCategoryName(SchoolCategoryEnum.KINDERGARTEN.getName());
                } else if (houseUsageRecordVO.getSchoolCategory().equals(SchoolCategoryEnum.PRIMARY.getType())) {
                    houseUsageRecordVO.setSchoolCategoryName(SchoolCategoryEnum.PRIMARY.getName());
                }else if (houseUsageRecordVO.getSchoolCategory().equals(SchoolCategoryEnum.JUNIOR.getType())){
                    houseUsageRecordVO.setSchoolCategoryName(SchoolCategoryEnum.JUNIOR.getName());
                }else {
                    houseUsageRecordVO.setSchoolCategoryName("");
                }
            } else {
                houseUsageRecordVO.setSchoolCategoryName("");
            }

            recordVOArrayList.add(houseUsageRecordVO);
        }
        eduHouseVO.setHouseUsageRecordVOList(recordVOArrayList);

        return eduHouseVO;
    }
    @Override
    @OpApi(funcCode = "eduStudentHouse0009", title = "家长端我的房产使用情况查询", funcType = FuncTypeEnum.query)
    public List<EduHouseVO> getMyUserHouse(EduStudentHouseQueryByRealpVO vo) {
        if(StringUtils.isBlank(vo.getAreaCode())){
            throw new AppException("所属行政区划不能为空");
        }
        String idcard = SessionUtil.getSessionInfo().getIdcard();
        CommonVO commonVO=new CommonVO();
        commonVO.setParamKey("local_house_year");
        commonVO.setAreaCode(vo.getAreaCode());
        String maxValue=commonIntegration.getValueByKey(commonVO);
        int max=StringUtils.isBlank(maxValue)?3:Integer.valueOf(maxValue);
        QueryWrapper<EduStudentHouse> houseQueryWrapper = new QueryWrapper<>();
        int nowYear = DateUtil.thisYear();
        houseQueryWrapper
                .lambda()
                .and(
                        wrapper -> {
                            wrapper.eq(EduStudentHouse::getFatherIdcard, idcard)
                                    .or()
                                    .eq(EduStudentHouse::getIdcard, idcard)
                                    .or()
                                    .eq(EduStudentHouse::getMotherIdcard, idcard);
                        })
                .ge(EduStudentHouse::getYear, nowYear - max)
                .eq(EduStudentHouse::getStatus, BoolEnum.TRUE.getType())
                .orderByDesc(EduStudentHouse::getYear);;
        List<EduStudentHouse> list = eduStudentHouseService.list(houseQueryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        Map<String, List<EduStudentHouse>> groupBy = list.stream().collect(Collectors.groupingBy(EduStudentHouse::getHouseId));
        List<EduHouseVO> houseVOList=new ArrayList<>();
        for(List<EduStudentHouse> houses:groupBy.values()){
            List<EduStudentHouseVO> voList=Convert.toList(EduStudentHouseVO.class,houses);
            EduHouseVO eduHouseVO=getUserHouse(voList,vo.getHiddenFlag());
            houseVOList.add(eduHouseVO);
        }
        return houseVOList;
    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0010", title = "学生房产使用情况导出", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void downloadData(StudentHouseQueryVO vo, HttpServletResponse response) {
        ExcelVO excelVO = new ExcelVO();
        excelVO.setModelCode(vo.getModelCode());
        ExcelModelVO excelModelVO=excelIntegration.getModel(excelVO);

        String destFilePath = this.tempPath + "/" + IdUtil.fastSimpleUUID() + "." + excelModelVO.getExcelType();
        int pageNum=1;
        setData(excelModelVO,null,destFilePath,vo,pageNum);
        String fileName=vo.getFileName();
        if (StringUtils.isBlank(fileName)) {
            fileName =
                    excelModelVO.getModelName()
                            + "_"
                            + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN)
                            + "."
                            + (StringUtils.isBlank(excelModelVO.getExcelType())
                            ? "xls"
                            : excelModelVO.getExcelType());
        }
        File file = FileUtil.file(destFilePath);
        BufferedInputStream in = null;
        try {
            in = FileUtil.getInputStream(file);
            ServletUtil.write(response, in, "application/octet-stream;charset=utf-8", fileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileUtil.del(destFilePath);
        }




    }

    @Override
    @OpApi(funcCode = "eduStudentHouse0011", title = "修改学生房产信息状态", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduStudentHouse0011",
            operationName = "修改学生房产信息状态",
            dataType = "studentHouseId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentHouseVO updateStatus(EduStudentHouseVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentHouseId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentHouse oldEntity = eduStudentHouseService.getById(vo.getStudentHouseId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        oldEntity.setStatus(vo.getStatus());
        eduStudentHouseService.updateById(oldEntity);
        clearCache(oldEntity.getRealProperty());
        return vo;
    }




    private void setData(ExcelModelVO excelModelVO, ExcelWriter excelWriter, String destFilePath, StudentHouseQueryVO vo, int pageNum){
        vo.setPageNum(pageNum);
        vo.setPageSize(500);
        boolean closeFlag = false;
        try {
            PageInfo<EduStudentHouseVO> pageInfo= queryStudentHouseInfo(vo);
            String total=String.valueOf(pageInfo.getTotal());
            List<Map<String,Object>> list=new ArrayList<>();
            if(!CollectionUtils.isEmpty(pageInfo.getList())){
                for(EduStudentHouseVO eduStudentHouseVO:pageInfo.getList()){
                    list.add(BeanUtil.beanToMap(eduStudentHouseVO,false,true));
                }
            }
            if(pageNum==1){
                if(excelWriter==null){
                    excelWriter=excelCommonHelper.createExcelWriter(excelModelVO,destFilePath,Integer.parseInt(total));
                }
                excelCommonHelper.writeMap(excelWriter,list);
            }else{
                excelCommonHelper.writeMap(excelWriter,list);
            }
            if(pageInfo.isHasNextPage()){
                pageNum=pageNum+1;
                setData(excelModelVO,excelWriter,destFilePath,vo,pageNum);
            }else {
                closeFlag = true;
            }
        }catch (AppException appEx){
            throw appEx;
        }catch (Exception e){
            throw new AppException("调用自定义写入服务异常:" + e.getMessage());
        }finally {
            if(closeFlag&&excelWriter!=null){
                excelWriter.close();
            }
        }
    }


    @Override
    @OpApi(funcCode = "useHouseCache0002", title = "清除房产使用查询缓存", funcType = FuncTypeEnum.other)
    public void clearCache(EduStudentHouseQueryByRealpVO vo) {
        if(StringUtils.isBlank(vo.getRealProperty())){
            throw new AppException("不动产权证号不能为空");
        }
        this.clearCache(vo.getRealProperty());
    }
    private void clearCache(String realProperty) {
        if(StringUtils.isBlank(realProperty)){
            return;
        }
        Cache cache = JetCacheUtils.create(EduCache.REAL_PROPERTY_USER_KEY, CacheType.BOTH);
        if (cache != null) {
            cache.remove(Convert.toDBC(realProperty));
        }
    }
}
